/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.admin.modeljpa;

import com.admin.modeljpa.exceptions.IllegalOrphanException;
import com.admin.modeljpa.exceptions.NonexistentEntityException;
import com.admin.modeljpa.exceptions.PreexistingEntityException;
import com.admin.modeljpa.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.entities.ContactMessage;
import com.entities.Employee;
import java.util.ArrayList;
import java.util.Collection;
import com.entities.EventRegistration;
import com.entities.PictureLibrary;
import com.entities.EventComment;
import com.entities.EventGift;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Tuan Long
 */
public class EmployeeJpaController implements Serializable {

    public EmployeeJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Employee employee) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (employee.getContactMessageCollection() == null) {
            employee.setContactMessageCollection(new ArrayList<ContactMessage>());
        }
        if (employee.getEventRegistrationCollection() == null) {
            employee.setEventRegistrationCollection(new ArrayList<EventRegistration>());
        }
        if (employee.getPictureLibraryCollection() == null) {
            employee.setPictureLibraryCollection(new ArrayList<PictureLibrary>());
        }
        if (employee.getEventCommentCollection() == null) {
            employee.setEventCommentCollection(new ArrayList<EventComment>());
        }
        if (employee.getEventGiftCollection() == null) {
            employee.setEventGiftCollection(new ArrayList<EventGift>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Collection<ContactMessage> attachedContactMessageCollection = new ArrayList<ContactMessage>();
            for (ContactMessage contactMessageCollectionContactMessageToAttach : employee.getContactMessageCollection()) {
                contactMessageCollectionContactMessageToAttach = em.getReference(contactMessageCollectionContactMessageToAttach.getClass(), contactMessageCollectionContactMessageToAttach.getId());
                attachedContactMessageCollection.add(contactMessageCollectionContactMessageToAttach);
            }
            employee.setContactMessageCollection(attachedContactMessageCollection);
            Collection<EventRegistration> attachedEventRegistrationCollection = new ArrayList<EventRegistration>();
            for (EventRegistration eventRegistrationCollectionEventRegistrationToAttach : employee.getEventRegistrationCollection()) {
                eventRegistrationCollectionEventRegistrationToAttach = em.getReference(eventRegistrationCollectionEventRegistrationToAttach.getClass(), eventRegistrationCollectionEventRegistrationToAttach.getId());
                attachedEventRegistrationCollection.add(eventRegistrationCollectionEventRegistrationToAttach);
            }
            employee.setEventRegistrationCollection(attachedEventRegistrationCollection);
            Collection<PictureLibrary> attachedPictureLibraryCollection = new ArrayList<PictureLibrary>();
            for (PictureLibrary pictureLibraryCollectionPictureLibraryToAttach : employee.getPictureLibraryCollection()) {
                pictureLibraryCollectionPictureLibraryToAttach = em.getReference(pictureLibraryCollectionPictureLibraryToAttach.getClass(), pictureLibraryCollectionPictureLibraryToAttach.getId());
                attachedPictureLibraryCollection.add(pictureLibraryCollectionPictureLibraryToAttach);
            }
            employee.setPictureLibraryCollection(attachedPictureLibraryCollection);
            Collection<EventComment> attachedEventCommentCollection = new ArrayList<EventComment>();
            for (EventComment eventCommentCollectionEventCommentToAttach : employee.getEventCommentCollection()) {
                eventCommentCollectionEventCommentToAttach = em.getReference(eventCommentCollectionEventCommentToAttach.getClass(), eventCommentCollectionEventCommentToAttach.getId());
                attachedEventCommentCollection.add(eventCommentCollectionEventCommentToAttach);
            }
            employee.setEventCommentCollection(attachedEventCommentCollection);
            Collection<EventGift> attachedEventGiftCollection = new ArrayList<EventGift>();
            for (EventGift eventGiftCollectionEventGiftToAttach : employee.getEventGiftCollection()) {
                eventGiftCollectionEventGiftToAttach = em.getReference(eventGiftCollectionEventGiftToAttach.getClass(), eventGiftCollectionEventGiftToAttach.getId());
                attachedEventGiftCollection.add(eventGiftCollectionEventGiftToAttach);
            }
            employee.setEventGiftCollection(attachedEventGiftCollection);
            em.persist(employee);
            for (ContactMessage contactMessageCollectionContactMessage : employee.getContactMessageCollection()) {
                Employee oldUsernameOfContactMessageCollectionContactMessage = contactMessageCollectionContactMessage.getUsername();
                contactMessageCollectionContactMessage.setUsername(employee);
                contactMessageCollectionContactMessage = em.merge(contactMessageCollectionContactMessage);
                if (oldUsernameOfContactMessageCollectionContactMessage != null) {
                    oldUsernameOfContactMessageCollectionContactMessage.getContactMessageCollection().remove(contactMessageCollectionContactMessage);
                    oldUsernameOfContactMessageCollectionContactMessage = em.merge(oldUsernameOfContactMessageCollectionContactMessage);
                }
            }
            for (EventRegistration eventRegistrationCollectionEventRegistration : employee.getEventRegistrationCollection()) {
                Employee oldUsernameOfEventRegistrationCollectionEventRegistration = eventRegistrationCollectionEventRegistration.getUsername();
                eventRegistrationCollectionEventRegistration.setUsername(employee);
                eventRegistrationCollectionEventRegistration = em.merge(eventRegistrationCollectionEventRegistration);
                if (oldUsernameOfEventRegistrationCollectionEventRegistration != null) {
                    oldUsernameOfEventRegistrationCollectionEventRegistration.getEventRegistrationCollection().remove(eventRegistrationCollectionEventRegistration);
                    oldUsernameOfEventRegistrationCollectionEventRegistration = em.merge(oldUsernameOfEventRegistrationCollectionEventRegistration);
                }
            }
            for (PictureLibrary pictureLibraryCollectionPictureLibrary : employee.getPictureLibraryCollection()) {
                Employee oldUsernameOfPictureLibraryCollectionPictureLibrary = pictureLibraryCollectionPictureLibrary.getUsername();
                pictureLibraryCollectionPictureLibrary.setUsername(employee);
                pictureLibraryCollectionPictureLibrary = em.merge(pictureLibraryCollectionPictureLibrary);
                if (oldUsernameOfPictureLibraryCollectionPictureLibrary != null) {
                    oldUsernameOfPictureLibraryCollectionPictureLibrary.getPictureLibraryCollection().remove(pictureLibraryCollectionPictureLibrary);
                    oldUsernameOfPictureLibraryCollectionPictureLibrary = em.merge(oldUsernameOfPictureLibraryCollectionPictureLibrary);
                }
            }
            for (EventComment eventCommentCollectionEventComment : employee.getEventCommentCollection()) {
                Employee oldUsernameOfEventCommentCollectionEventComment = eventCommentCollectionEventComment.getUsername();
                eventCommentCollectionEventComment.setUsername(employee);
                eventCommentCollectionEventComment = em.merge(eventCommentCollectionEventComment);
                if (oldUsernameOfEventCommentCollectionEventComment != null) {
                    oldUsernameOfEventCommentCollectionEventComment.getEventCommentCollection().remove(eventCommentCollectionEventComment);
                    oldUsernameOfEventCommentCollectionEventComment = em.merge(oldUsernameOfEventCommentCollectionEventComment);
                }
            }
            for (EventGift eventGiftCollectionEventGift : employee.getEventGiftCollection()) {
                Employee oldUsernameOfEventGiftCollectionEventGift = eventGiftCollectionEventGift.getUsername();
                eventGiftCollectionEventGift.setUsername(employee);
                eventGiftCollectionEventGift = em.merge(eventGiftCollectionEventGift);
                if (oldUsernameOfEventGiftCollectionEventGift != null) {
                    oldUsernameOfEventGiftCollectionEventGift.getEventGiftCollection().remove(eventGiftCollectionEventGift);
                    oldUsernameOfEventGiftCollectionEventGift = em.merge(oldUsernameOfEventGiftCollectionEventGift);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findEmployee(employee.getUsername()) != null) {
                throw new PreexistingEntityException("Employee " + employee + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Employee employee) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Employee persistentEmployee = em.find(Employee.class, employee.getUsername());
            Collection<ContactMessage> contactMessageCollectionOld = persistentEmployee.getContactMessageCollection();
            Collection<ContactMessage> contactMessageCollectionNew = employee.getContactMessageCollection();
            Collection<EventRegistration> eventRegistrationCollectionOld = persistentEmployee.getEventRegistrationCollection();
            Collection<EventRegistration> eventRegistrationCollectionNew = employee.getEventRegistrationCollection();
            Collection<PictureLibrary> pictureLibraryCollectionOld = persistentEmployee.getPictureLibraryCollection();
            Collection<PictureLibrary> pictureLibraryCollectionNew = employee.getPictureLibraryCollection();
            Collection<EventComment> eventCommentCollectionOld = persistentEmployee.getEventCommentCollection();
            Collection<EventComment> eventCommentCollectionNew = employee.getEventCommentCollection();
            Collection<EventGift> eventGiftCollectionOld = persistentEmployee.getEventGiftCollection();
            Collection<EventGift> eventGiftCollectionNew = employee.getEventGiftCollection();
            List<String> illegalOrphanMessages = null;
            for (ContactMessage contactMessageCollectionOldContactMessage : contactMessageCollectionOld) {
                if (!contactMessageCollectionNew.contains(contactMessageCollectionOldContactMessage)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ContactMessage " + contactMessageCollectionOldContactMessage + " since its username field is not nullable.");
                }
            }
            for (EventRegistration eventRegistrationCollectionOldEventRegistration : eventRegistrationCollectionOld) {
                if (!eventRegistrationCollectionNew.contains(eventRegistrationCollectionOldEventRegistration)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EventRegistration " + eventRegistrationCollectionOldEventRegistration + " since its username field is not nullable.");
                }
            }
            for (PictureLibrary pictureLibraryCollectionOldPictureLibrary : pictureLibraryCollectionOld) {
                if (!pictureLibraryCollectionNew.contains(pictureLibraryCollectionOldPictureLibrary)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PictureLibrary " + pictureLibraryCollectionOldPictureLibrary + " since its username field is not nullable.");
                }
            }
            for (EventComment eventCommentCollectionOldEventComment : eventCommentCollectionOld) {
                if (!eventCommentCollectionNew.contains(eventCommentCollectionOldEventComment)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EventComment " + eventCommentCollectionOldEventComment + " since its username field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<ContactMessage> attachedContactMessageCollectionNew = new ArrayList<ContactMessage>();
            for (ContactMessage contactMessageCollectionNewContactMessageToAttach : contactMessageCollectionNew) {
                contactMessageCollectionNewContactMessageToAttach = em.getReference(contactMessageCollectionNewContactMessageToAttach.getClass(), contactMessageCollectionNewContactMessageToAttach.getId());
                attachedContactMessageCollectionNew.add(contactMessageCollectionNewContactMessageToAttach);
            }
            contactMessageCollectionNew = attachedContactMessageCollectionNew;
            employee.setContactMessageCollection(contactMessageCollectionNew);
            Collection<EventRegistration> attachedEventRegistrationCollectionNew = new ArrayList<EventRegistration>();
            for (EventRegistration eventRegistrationCollectionNewEventRegistrationToAttach : eventRegistrationCollectionNew) {
                eventRegistrationCollectionNewEventRegistrationToAttach = em.getReference(eventRegistrationCollectionNewEventRegistrationToAttach.getClass(), eventRegistrationCollectionNewEventRegistrationToAttach.getId());
                attachedEventRegistrationCollectionNew.add(eventRegistrationCollectionNewEventRegistrationToAttach);
            }
            eventRegistrationCollectionNew = attachedEventRegistrationCollectionNew;
            employee.setEventRegistrationCollection(eventRegistrationCollectionNew);
            Collection<PictureLibrary> attachedPictureLibraryCollectionNew = new ArrayList<PictureLibrary>();
            for (PictureLibrary pictureLibraryCollectionNewPictureLibraryToAttach : pictureLibraryCollectionNew) {
                pictureLibraryCollectionNewPictureLibraryToAttach = em.getReference(pictureLibraryCollectionNewPictureLibraryToAttach.getClass(), pictureLibraryCollectionNewPictureLibraryToAttach.getId());
                attachedPictureLibraryCollectionNew.add(pictureLibraryCollectionNewPictureLibraryToAttach);
            }
            pictureLibraryCollectionNew = attachedPictureLibraryCollectionNew;
            employee.setPictureLibraryCollection(pictureLibraryCollectionNew);
            Collection<EventComment> attachedEventCommentCollectionNew = new ArrayList<EventComment>();
            for (EventComment eventCommentCollectionNewEventCommentToAttach : eventCommentCollectionNew) {
                eventCommentCollectionNewEventCommentToAttach = em.getReference(eventCommentCollectionNewEventCommentToAttach.getClass(), eventCommentCollectionNewEventCommentToAttach.getId());
                attachedEventCommentCollectionNew.add(eventCommentCollectionNewEventCommentToAttach);
            }
            eventCommentCollectionNew = attachedEventCommentCollectionNew;
            employee.setEventCommentCollection(eventCommentCollectionNew);
            Collection<EventGift> attachedEventGiftCollectionNew = new ArrayList<EventGift>();
            for (EventGift eventGiftCollectionNewEventGiftToAttach : eventGiftCollectionNew) {
                eventGiftCollectionNewEventGiftToAttach = em.getReference(eventGiftCollectionNewEventGiftToAttach.getClass(), eventGiftCollectionNewEventGiftToAttach.getId());
                attachedEventGiftCollectionNew.add(eventGiftCollectionNewEventGiftToAttach);
            }
            eventGiftCollectionNew = attachedEventGiftCollectionNew;
            employee.setEventGiftCollection(eventGiftCollectionNew);
            employee = em.merge(employee);
            for (ContactMessage contactMessageCollectionNewContactMessage : contactMessageCollectionNew) {
                if (!contactMessageCollectionOld.contains(contactMessageCollectionNewContactMessage)) {
                    Employee oldUsernameOfContactMessageCollectionNewContactMessage = contactMessageCollectionNewContactMessage.getUsername();
                    contactMessageCollectionNewContactMessage.setUsername(employee);
                    contactMessageCollectionNewContactMessage = em.merge(contactMessageCollectionNewContactMessage);
                    if (oldUsernameOfContactMessageCollectionNewContactMessage != null && !oldUsernameOfContactMessageCollectionNewContactMessage.equals(employee)) {
                        oldUsernameOfContactMessageCollectionNewContactMessage.getContactMessageCollection().remove(contactMessageCollectionNewContactMessage);
                        oldUsernameOfContactMessageCollectionNewContactMessage = em.merge(oldUsernameOfContactMessageCollectionNewContactMessage);
                    }
                }
            }
            for (EventRegistration eventRegistrationCollectionNewEventRegistration : eventRegistrationCollectionNew) {
                if (!eventRegistrationCollectionOld.contains(eventRegistrationCollectionNewEventRegistration)) {
                    Employee oldUsernameOfEventRegistrationCollectionNewEventRegistration = eventRegistrationCollectionNewEventRegistration.getUsername();
                    eventRegistrationCollectionNewEventRegistration.setUsername(employee);
                    eventRegistrationCollectionNewEventRegistration = em.merge(eventRegistrationCollectionNewEventRegistration);
                    if (oldUsernameOfEventRegistrationCollectionNewEventRegistration != null && !oldUsernameOfEventRegistrationCollectionNewEventRegistration.equals(employee)) {
                        oldUsernameOfEventRegistrationCollectionNewEventRegistration.getEventRegistrationCollection().remove(eventRegistrationCollectionNewEventRegistration);
                        oldUsernameOfEventRegistrationCollectionNewEventRegistration = em.merge(oldUsernameOfEventRegistrationCollectionNewEventRegistration);
                    }
                }
            }
            for (PictureLibrary pictureLibraryCollectionNewPictureLibrary : pictureLibraryCollectionNew) {
                if (!pictureLibraryCollectionOld.contains(pictureLibraryCollectionNewPictureLibrary)) {
                    Employee oldUsernameOfPictureLibraryCollectionNewPictureLibrary = pictureLibraryCollectionNewPictureLibrary.getUsername();
                    pictureLibraryCollectionNewPictureLibrary.setUsername(employee);
                    pictureLibraryCollectionNewPictureLibrary = em.merge(pictureLibraryCollectionNewPictureLibrary);
                    if (oldUsernameOfPictureLibraryCollectionNewPictureLibrary != null && !oldUsernameOfPictureLibraryCollectionNewPictureLibrary.equals(employee)) {
                        oldUsernameOfPictureLibraryCollectionNewPictureLibrary.getPictureLibraryCollection().remove(pictureLibraryCollectionNewPictureLibrary);
                        oldUsernameOfPictureLibraryCollectionNewPictureLibrary = em.merge(oldUsernameOfPictureLibraryCollectionNewPictureLibrary);
                    }
                }
            }
            for (EventComment eventCommentCollectionNewEventComment : eventCommentCollectionNew) {
                if (!eventCommentCollectionOld.contains(eventCommentCollectionNewEventComment)) {
                    Employee oldUsernameOfEventCommentCollectionNewEventComment = eventCommentCollectionNewEventComment.getUsername();
                    eventCommentCollectionNewEventComment.setUsername(employee);
                    eventCommentCollectionNewEventComment = em.merge(eventCommentCollectionNewEventComment);
                    if (oldUsernameOfEventCommentCollectionNewEventComment != null && !oldUsernameOfEventCommentCollectionNewEventComment.equals(employee)) {
                        oldUsernameOfEventCommentCollectionNewEventComment.getEventCommentCollection().remove(eventCommentCollectionNewEventComment);
                        oldUsernameOfEventCommentCollectionNewEventComment = em.merge(oldUsernameOfEventCommentCollectionNewEventComment);
                    }
                }
            }
            for (EventGift eventGiftCollectionOldEventGift : eventGiftCollectionOld) {
                if (!eventGiftCollectionNew.contains(eventGiftCollectionOldEventGift)) {
                    eventGiftCollectionOldEventGift.setUsername(null);
                    eventGiftCollectionOldEventGift = em.merge(eventGiftCollectionOldEventGift);
                }
            }
            for (EventGift eventGiftCollectionNewEventGift : eventGiftCollectionNew) {
                if (!eventGiftCollectionOld.contains(eventGiftCollectionNewEventGift)) {
                    Employee oldUsernameOfEventGiftCollectionNewEventGift = eventGiftCollectionNewEventGift.getUsername();
                    eventGiftCollectionNewEventGift.setUsername(employee);
                    eventGiftCollectionNewEventGift = em.merge(eventGiftCollectionNewEventGift);
                    if (oldUsernameOfEventGiftCollectionNewEventGift != null && !oldUsernameOfEventGiftCollectionNewEventGift.equals(employee)) {
                        oldUsernameOfEventGiftCollectionNewEventGift.getEventGiftCollection().remove(eventGiftCollectionNewEventGift);
                        oldUsernameOfEventGiftCollectionNewEventGift = em.merge(oldUsernameOfEventGiftCollectionNewEventGift);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = employee.getUsername();
                if (findEmployee(id) == null) {
                    throw new NonexistentEntityException("The employee with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Employee employee;
            try {
                employee = em.getReference(Employee.class, id);
                employee.getUsername();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The employee with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<ContactMessage> contactMessageCollectionOrphanCheck = employee.getContactMessageCollection();
            for (ContactMessage contactMessageCollectionOrphanCheckContactMessage : contactMessageCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employee (" + employee + ") cannot be destroyed since the ContactMessage " + contactMessageCollectionOrphanCheckContactMessage + " in its contactMessageCollection field has a non-nullable username field.");
            }
            Collection<EventRegistration> eventRegistrationCollectionOrphanCheck = employee.getEventRegistrationCollection();
            for (EventRegistration eventRegistrationCollectionOrphanCheckEventRegistration : eventRegistrationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employee (" + employee + ") cannot be destroyed since the EventRegistration " + eventRegistrationCollectionOrphanCheckEventRegistration + " in its eventRegistrationCollection field has a non-nullable username field.");
            }
            Collection<PictureLibrary> pictureLibraryCollectionOrphanCheck = employee.getPictureLibraryCollection();
            for (PictureLibrary pictureLibraryCollectionOrphanCheckPictureLibrary : pictureLibraryCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employee (" + employee + ") cannot be destroyed since the PictureLibrary " + pictureLibraryCollectionOrphanCheckPictureLibrary + " in its pictureLibraryCollection field has a non-nullable username field.");
            }
            Collection<EventComment> eventCommentCollectionOrphanCheck = employee.getEventCommentCollection();
            for (EventComment eventCommentCollectionOrphanCheckEventComment : eventCommentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employee (" + employee + ") cannot be destroyed since the EventComment " + eventCommentCollectionOrphanCheckEventComment + " in its eventCommentCollection field has a non-nullable username field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<EventGift> eventGiftCollection = employee.getEventGiftCollection();
            for (EventGift eventGiftCollectionEventGift : eventGiftCollection) {
                eventGiftCollectionEventGift.setUsername(null);
                eventGiftCollectionEventGift = em.merge(eventGiftCollectionEventGift);
            }
            em.remove(employee);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Employee> findEmployeeEntities() {
        return findEmployeeEntities(true, -1, -1);
    }

    public List<Employee> findEmployeeEntities(int maxResults, int firstResult) {
        return findEmployeeEntities(false, maxResults, firstResult);
    }

    private List<Employee> findEmployeeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Employee.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Employee findEmployee(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Employee.class, id);
        } finally {
            em.close();
        }
    }

    public int getEmployeeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Employee> rt = cq.from(Employee.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
